﻿

namespace jQube.Web
{
    using System.Web.UI;
    using System.IO;
    using System.Text;

    internal class ParserHtmlTextWriter : HtmlTextWriter
    {
        private bool writingForm;
        private StringBuilder jsonParsedForm;

        public ParserHtmlTextWriter(TextWriter writer)
            : base(writer)
        {
            this.writingForm = false;
            this.jsonParsedForm = new StringBuilder();
        }

        public string JSonParsedForm
        {
            get
            {
                return this.jsonParsedForm.ToString();
            }
        }

        public override void WriteBeginTag(string tagName)
        {
            base.WriteBeginTag(tagName);
            this.writingForm = (tagName == "form");
        }

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            base.WriteAttribute(name, value, fEncode);

            if (this.writingForm)
            {
                switch (name)
                {
                    case "id":
                        this.jsonParsedForm.AppendFormat("\"name\":\"{0}\"", value);
                        break;
                    case "action":
                        this.jsonParsedForm.AppendFormat("\"action\":\"{0}\",", value);
                        break;
                    case "method":
                        this.jsonParsedForm.AppendFormat("\"method\":\"{0}\",", value);
                        break;
                }
            }
        }
    }
}
